﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;
using System.Text;
using System.ComponentModel;
using System.Collections;


namespace GestionIncidencias.Models
{
    public class ServicioModel
    {
 
        /* Modelo y Validaciones  */
        [DisplayName("Codigo")]
        public string Codigo { get; set; }

        //public IEnumerable Nombres { get; set; }
        [DisplayName("Nombre")]
        public string Nombre { get; set; }

        //public IEnumerable Descripciones { get; set; }
        [DisplayName("Descripcion")]
        public string Descripcion { get; set; }
         

        /*Metodos Rest Crud */
        public List<Servicio> lstServicio = null;


        public string nuevoServico(Servicio servicio)
        {

            JavaScriptSerializer js = new JavaScriptSerializer();
            string postdata = js.Serialize(servicio);
            byte[] data = Encoding.UTF8.GetBytes(postdata);

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8090/ServicioServices.svc/servicios");
            req.Method = "POST";
            req.ContentLength = data.Length;
            req.ContentType = "application/json";

            var reqStream = req.GetRequestStream();
            reqStream.Write(data, 0, data.Length);

            //reqStream.Close();
            var res = (HttpWebResponse)req.GetResponse();
            //StreamReader reader = new StreamReader(res.GetResponseStream());
            //string servicioJson = reader.ReadToEnd();
            //return js.Deserialize<string>(servicioJson);
            return ("ListarServicio");


        }


        public string actualizarServicio(Servicio servicio)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            string postdata = js.Serialize(servicio);
            byte[] data = Encoding.UTF8.GetBytes(postdata);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8090/ServicioServices.svc/servicios");
            req.Method = "PUT";
            req.ContentLength = data.Length;
            req.ContentType = "application/json";
            var reqStream = req.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();
            var res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string servicioJson = reader.ReadToEnd();
            return js.Deserialize<string>(servicioJson);
        }

        public void obtenerServicio(string codigo)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8090/ServicioServices.svc/servicios/"+ codigo);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string servicioJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            Servicio servicioObtenido = js.Deserialize<Servicio>(servicioJson);
            this.Codigo = servicioObtenido.Codigo.ToString();
            this.Nombre = servicioObtenido.Nombre;
            this.Descripcion = servicioObtenido.Descripcion;
        }

        public void eliminarServicio(string codigo)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            string postdata = js.Serialize(codigo);
            byte[] data = Encoding.UTF8.GetBytes(postdata);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8090/ServicioServices.svc/servicios/" + codigo);
            req.Method = "DELETE";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();            
            req.ContentLength = data.Length;
            req.ContentType = "application/json";
            var reqStream = req.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            //var res = (HttpWebResponse)req.GetResponse();


        }

        public void ListarServicio()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8090/ServicioServices.svc/servicios");
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string servicioJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            this.lstServicio = js.Deserialize<List<Servicio>>(servicioJson);
        }

    }
}